﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;
namespace BusinessLogic
{
  public static  class ChiTietKiemKeKho
    {
      public static Entities.ChiTietKiemKeKho[] Select(Entities.ChiTietKiemKeKho nv)
        {
            Entities.ChiTietKiemKeKho[] list = null;
            try
            {
                string sql = Common.Constants.ChiTietKiemKeKho.SelectChiTietKiemKeKho;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietKiemKeKho.HanhDong, SqlDbType.NVarChar).Value = nv.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietKiemKeKho.MaPhieuKiemKe, SqlDbType.VarChar).Value = nv.MaPhieuKiemKe;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietKiemKeKho row = new Entities.ChiTietKiemKeKho();
                    row.ChiTietKiemKeKhoID = int.Parse(dr[Common.Constants.ChiTietKiemKeKho.ChiTietKiemKeKhoID].ToString());
                    row.MaPhieuKiemKe = dr[Common.Constants.ChiTietKiemKeKho.MaPhieuKiemKe].ToString();
                    row.MaHangHoa = dr[Common.Constants.ChiTietKiemKeKho.MaHangHoa].ToString();
                    row.TenHangHoa = dr[Common.Constants.ChiTietKiemKeKho.TenHangHoa].ToString();
                    row.TonThucTe = int.Parse(dr[Common.Constants.ChiTietKiemKeKho.TonThucTe].ToString());
                    row.TonSoSach = int.Parse(dr[Common.Constants.ChiTietKiemKeKho.TonSoSach].ToString());
                    row.LyDo = dr[Common.Constants.ChiTietKiemKeKho.LyDo].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.ChiTietKiemKeKho.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.ChiTietKiemKeKho[0];
                }
                else
                {
                    list = new Entities.ChiTietKiemKeKho[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.ChiTietKiemKeKho)arr[i];
                    }
                }

                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.ChiTietKiemKeKho[0]; }
            return list;
        }
      public static int InsertUpdate(Entities.ChiTietKiemKeKho nhom)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.ChiTietKiemKeKho.InsertUpdateChiTietKiemKeKho, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietKiemKeKho.HanhDong, SqlDbType.NVarChar).Value = nhom.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietKiemKeKho.ChiTietKiemKeKhoID, SqlDbType.Int).Value = nhom.ChiTietKiemKeKhoID;
                cmd.Parameters.Add(Common.Constants.ChiTietKiemKeKho.MaPhieuKiemKe, SqlDbType.VarChar, 50).Value = nhom.MaPhieuKiemKe;
                cmd.Parameters.Add(Common.Constants.ChiTietKiemKeKho.MaHangHoa, SqlDbType.VarChar, 50).Value = nhom.MaHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietKiemKeKho.TenHangHoa, SqlDbType.NVarChar, 200).Value = nhom.TenHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietKiemKeKho.TonThucTe, SqlDbType.Int).Value = nhom.TonThucTe;
                cmd.Parameters.Add(Common.Constants.ChiTietKiemKeKho.TonSoSach, SqlDbType.Int).Value = nhom.TonSoSach;
                cmd.Parameters.Add(Common.Constants.ChiTietKiemKeKho.LyDo, SqlDbType.NVarChar, 200).Value = nhom.LyDo;
                cmd.Parameters.Add(Common.Constants.ChiTietKiemKeKho.Deleted, SqlDbType.Bit).Value = nhom.Deleted;
                rt = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); rt = 0; }
            return rt;
        }
        public static int InsertUpdate(Entities.ChiTietKiemKeKho[] chitietkiemkekho)
        {
            try
            {
                foreach (Entities.ChiTietKiemKeKho item in chitietkiemkekho)
                {
                    if (InsertUpdate(item) == 0)
                    {
                        return 0;
                    }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        public static int Delete(Entities.ChiTietKiemKeKho nhom)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.ChiTietKiemKeKho.DeleteChiTietKiemKeKho, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietKiemKeKho.ChiTietKiemKeKhoID, SqlDbType.Int).Value = nhom.ChiTietKiemKeKhoID;
                rt = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); rt = 0; }
            return rt;
        }
    }
}
